#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Main {
	int n, m;
	vector<int> father;
	vector<int> relation;

	int find(int x)
	{
		if (x != father[x]) {
			int t = father[x];
			father[x] = find(father[x]);
			relation[x] = (relation[x] + relation[t]) % 3;
		}
		return father[x];
	}

	int unite(int x, int y, int v)
	{
		int fx = find(x), fy = find(y);
		if (fx != fy) {
			father[fx] = fy;
			relation[fx] = (relation[y] - relation[x] + v + 3) % 3;
			return 1;
		}
		return 0;
	}

    public:
	Main(int n, int m) : n(n), m(m), father(n + 1, 0), relation(n + 1, 0)
	{
		iota(father.begin(), father.end(), 0);
	}
	void run()
	{
		int rel = 0, x = 0, y = 0, ans = 0;
		for (int i = 0, fx, fy; i < m; i++) {
			cin >> rel >> x >> y;
			if (x > n || y > n || (rel == 2 && x == y)) {
				ans++;
				continue;
			}

			fx = find(x);
			fy = find(y);

			if (fx != fy) {
				unite(x, y, rel == 1 ? 0 : 1);
				continue;
			}

			if (rel == 1) {
				if (relation[x] != relation[y]) {
					ans++;
				}
			} else {
				if (((relation[x] - relation[y] + 3) % 3) != 1) {
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
};

int main()
{
	int n = 0, m = 0;
	cin >> n >> m;
	Main app(n, m);
	app.run();
	return 0;
}